package com.hdcircles.autoform.ui.label.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hdcircles.autoform.R;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by hdcircles on 2016/11/18.
 */

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder> {

    private List<User> users;

    private List<User> selectedUser;

    private onItemClickListener onItemClickListener;

    public UserListAdapter(){
        selectedUser=new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_list,parent,false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final User user=users.get(position);

        holder.itemView.setEnabled(!user.isShared());

        holder.ivUser.setImageDrawable(holder.itemView.getContext().getDrawable(R.drawable.icon));
        if (null!=user.getPhoto()&&0!=user.getPhoto().length()){
            Glide.with(holder.itemView.getContext())
                    .load(user.getPhoto())
                    .into(holder.ivUser);
        }
        holder.tvUser.setText(user.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User itemUser=users.get(holder.getAdapterPosition());
                onItemClickListener.onItemClick(itemUser);
            }
        });
        holder.checkBox.setChecked(user.isSelected());
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (user.isShared()){
                    holder.checkBox.setChecked(true);
                    return;
                }
                User updateUser=users.get(holder.getAdapterPosition());
                onItemClickListener.onItemClick(updateUser);
            }
        });
    }

    public List<User> getSelectedUser() {
        return selectedUser;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void setOnItemClickListener(UserListAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface onItemClickListener{

        void onItemClick(User user);
    }

    @Override
    public int getItemCount() {
        return null==users?0:users.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.iv_user)
        CircleImageView ivUser;
        @BindView(R.id.tv_user)
        TextView tvUser;
        @BindView(R.id.checkbox)
        CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
